import threading
from time import sleep,ctime
loops=[4,2]
class ThreadFunc(object):
	def __init__(self,func,args,name=''):
		self.name=name
		self.func = func
		self.args=args
		
	def __call__(self):
		self.func(*self.args)
def loop(nloop,nsec):
	print('开始循环',nloop,'at:',ctime())
	sleep(nsec)
	print('循环',nloop,'结束于：',ctime())
def main():
	print('程序开始于：',ctime())
	threads=[]
	nloops=range(len(loops))
	for i in nloops:
		t = threading.Thread(target=ThreadFunc(loop,(i,loops[i]),loop.__name__)) #传递一个可调用类的实例
		threads.append(t)
	for i in nloops:
		threads[i].start()  #循环 开始线程
	for i in nloops:
		threads[i].join()   #循环 join()方法可以让主线程等待所有的线程都执行完毕。
	print('任务完成于：',ctime())
	
if __name__=='__main__':
	main()